//*****************************************************************************
//  172x72 COG Display Temp. Comp. FW
//  MCU: MSP430F235
//  PCB: 10424
//  Compiler: TI Code Composer Essentials MSP430 v3.1
//  Kent Displays, Inc.
//  Copyright (c) 2009. All rights reserved worldwide.
//  Revisions
//     A - Initial. (24V Drive System, No Focal Conic Erase)
//     B - 28V Drive System, Focal Conic Erase
//*****************************************************************************

#include "Driver/system.h"
#include "Driver/mystring.h" 
#include "Driver/hal_ucs.h"
#include "string.h"

#define UART_DEBUG
#define RTC_DEBUG
#define RF_DEBUG
//#define LCD_DEBUG
#define LED_DEBUG
//#define GT_DEBUG

#ifdef GT_DEBUG
#include "Driver/GT22.h"
#endif

#ifdef LED_DEBUG
#include "Driver/led.h" 
#endif

#ifdef LED_DEBUG
#define LED1_RESET() LED1_reset()
#define LED2_RESET() LED2_reset()
#define LED1_TOGGLE() LED1_toggle()
#define LED2_TOGGLE() LED2_toggle()
#define LED1_SET() LED1_set()
#define LED2_SET() LED2_set()
#else
#define LED1_RESET()
#define LED2_RESET() 
#define LED1_TOGGLE()
#define LED2_TOGGLE() 
#define LED1_SET() 
#define LED2_SET()
#endif

#ifdef RF_DEBUG
#include "Driver/CC2500.h"
#endif

#ifdef RTC_DEBUG
#include "Driver/rtc.h"
#endif

#ifdef UART_DEBUG
#include "Driver/uart.h"
#define PRINT_CHAR(x) print_char(x) 
#define PRINT(x) print(x)
#define PRINT_RF(x, length) print_rf(x, length)
#else
#define PRINT_CHAR(x)
#define PRINT(x)
#define PRINT_RF(x, length)
#endif

/********** reply *************/

#define REPLY_OK 0x00
#define REPLY_ERR 0x01
#define REPLY_LOSE 0x03

enum { UPDATETYPE = 0x64, TIMETYPE = 0x65, INTERVALTYPE = 0x66 , WORKTIMETYPE = 0x67, ESLADDRESSTYPE = 0x68 };
unsigned char MessageType;

typedef enum {  LPM4_MODE, LPM3_MODE, ACTIVE_MODE, APPLICATION_MODE } McuStatus;
volatile McuStatus CpuMode = ACTIVE_MODE;

typedef enum {  TURE = 0, FALSE = !TURE } FlagStatus;

unsigned char txBuffer[64];
unsigned char rf_rxBuffer[64];

#ifdef UART_DEBUG
static volatile FlagStatus uart_receive_flag = TURE;
#endif

volatile FlagStatus interrup_time = TURE;

#ifdef RF_DEBUG
static volatile FlagStatus rf_receive_flag = TURE;
#endif

volatile unsigned char data_count = 0;

void main(void)
{   
	unsigned short i = 0;
	// System Initialization
	StopWatchdog();               // Stop watchdog timer.
	ConfigPorts();                // Configure I/O ports.
//	ConfigClocks();               // Configure basic clock module.
    
    P5SEL |= BIT4 + BIT5;                       // Port select XT1
 
	//Initialize clock and peripherals
	LFXT_Start(XT1DRIVE_3, XCAP_3);
    Init_FLL_Settle(8000, 244);
    SFRIE1 |= OFIE;

#ifdef LED_DEBUG    
    LED_config();
#endif
    
#ifdef UART_DEBUG
     UART_config();
     PRINT("hello world!");
#endif

#ifdef RF_DEBUG
    SPI_SELECT();
    SPI_SET_CLK();
    SPI_DIS_SELECT();
    cc2500_Init(0x00);
    Set_CCxx00Rxd();
#endif

#ifdef RF_DEBUG

    P2IES |= 0x01;
    P2IFG &= ~0x01;
    P2IE  |= 0x01;
#endif

    _BIS_SR(GIE);

	while (1) {
		
        if (uart_receive_flag == FALSE) {
            uart_receive_flag = TURE;
			
            bspIState_t s;
			BSP_ENTER_CRITICAL_SECTION(s);
            P2IE &= ~0x01;
            P2IFG &= ~0x01;                 // Clear flag
            
            for (i = 0; i < data_count; i++) {
                print_char(rxBuffer[i]);
            }

            RFSendPacket(rxBuffer, data_count);
            
            str_set(rxBuffer, 64, 0x00);
            
            LED1_toggle();
            
            Set_CCxx00Rxd();
            P2IE |= 0x01;
            BSP_EXIT_CRITICAL_SECTION(s);
        }
        
        if (rf_receive_flag == FALSE) {
			rf_receive_flag = TURE;
            
            bspIState_t s;
			BSP_ENTER_CRITICAL_SECTION(s);
			
            LED2_toggle();

			for (i = 0; i < rf_rxBuffer[0] + 1; i++) {
				print_char(rf_rxBuffer[i]);
			}

			str_set(rf_rxBuffer, 64, 0x00);

			for (i = 0; i < 6000; i++);
            Set_CCxx00Rxd();
            P2IE |= 0x01;
			BSP_EXIT_CRITICAL_SECTION(s);
		}
	}
}

#pragma vector =PORT2_VECTOR
__interrupt void P2_sever()
{
    P2IE &= ~0x01;
    P2IFG &= ~0x01;                 // Clear flag
    
    RFReceivePacket(rf_rxBuffer);

    LED2_toggle();
    
    if (rf_receive_flag == TURE) {
        rf_receive_flag = FALSE;
    } 
}

#ifdef UART_DEBUG

unsigned char k = 0x00;
unsigned char c = 0x00;

#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR (void)
{
    switch (__even_in_range(UCA1IV,4)){
        //Vector 2 - RXIFG
        case 2:

            //USCI_A0 TX buffer ready?
            while (!UART_getInterruptStatus(UCTXIFG));
            c = UART_receiveData();
//            print_char(c);
			TxBuffer[k] = c;
	
			if ((TxBuffer[k - 2] == 0x0D) && (TxBuffer[k - 1] == 0x0D) && (TxBuffer[k] == 0x0D)) {
                if ((uart_receive_flag == TURE)) {
    			    uart_receive_flag = FALSE;
					data_count = k - 2;
					memcpy(rxBuffer, TxBuffer, data_count);
					memset(TxBuffer, 0x00, 64);
					k = 0;
			    }
			} else {
                if (k++ > 64) {
                    k = 0;
                }				
			}
			
            break;
        default: break;
    }
}

#endif

#pragma vector=UNMI_VECTOR
__interrupt void UNMI_ISR(void)
{
    switch (SYSUNIV)
    {
        case 0x0004:  LFXT_Start(XT1DRIVE_3, XCAP_3); break;
        default: break;
    }
}